class ReporterVideosController < ApplicationController
  before_filter :authenticate
  layout "reporter"
  
  # GET /images
  # GET /images.xml
  def index
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @filter = params[:filter].nil? ? {} : params[:filter]
    @videos = Video.reporter_videos_list(current_user, @filter).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 5
	
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @images }
    end
  end

  # GET /images/1
  # GET /images/1.xml
  def show
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @image = Image.find(params[:id])
	@event = Event.all
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @image }
    end
  end

  # GET /images/new
  # GET /images/new.xml
  def new
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @video = Video.new
    @events = Event.all.collect {|p| [ p.title, p.id ] }
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @image }
    end
  end

  # GET /images/1/edit
  def edit
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @video = Video.find(params[:id])
    @events = Event.all.collect {|p| [ p.title, p.id ] }

  end

  # PUT /images/1
  # PUT /images/1.xml
  def update
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    

    @video = Video.find(params[:id])
    
    
    thumb = params[:video_thumbnail]    
    if !thumb.nil?
      @video.deleteVideoThumbnail
      @video.createVideoThumbnail(thumb)
      @video.thumbnail = thumb.original_filename
    end
    
    

    respond_to do |format|
      if @video.update_attributes(params[:video])
        format.html { redirect_to(url_for(:action => 'index'), :notice => 'Video was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @image.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def save
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
     
    if !params[:video_file].nil?
      @video = Video.new(params[:video])
      
      # Add event id
      @video.event_id = params[:video][:event_id]
      
      # add current user
      @video.user = current_user
      
      # video thumbnail params
      vid = params[:video_file]
      thumb = params[:video_thumbnail]
      
      #attributes
      @video.title = vid.original_filename if @video.title.empty? || @video.title.nil?
      @video.path = vid.original_filename
      @video.thumbnail = thumb.original_filename
      
      @video.save
      
      # upload Video
      @video.upload(vid, thumb)
      
      # create thumbnail     
      
    end    
      
    respond_to do |format|

      format.html { redirect_to(url_for(:action => 'index'), :notice => 'Images was successfully added.') }
      format.xml  { head :ok }
 
    end

  end
 
	

  # DELETE /images/1
  def destroy
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
	@video = Video.find(params[:id])
	@video.deleteVideo
	@video.destroy
	
	
	respond_to do |format|
          format.html { redirect_to(url_for(:action => 'index')) }
          format.xml  { head :ok }
        end
  end
  
  def delete_selected
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('REPORTER'))
    
    @ids = params[:ids]
    @user = current_user
    
      @ids.each do |obj|
      @video = Video.find(obj[0])
      @video.deleteVideo
      @video.destroy          
  end
    
    
    respond_to do |format|
        format.html { redirect_to({:action => 'index'}, :notice => "Images was successfully deleted.") }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
  def small_gallery
    render :layout => false
  end
end
